//
//  SpecialAlertView.m
//  Abc Genius Pro
//
//  Created by 连横合创 on 2018/11/7.
//  Copyright © 2018年 leewei. All rights reserved.
//

#define ALERTVIEW_HEIGHT [UIScreen mainScreen].bounds.size.height/3
#define ALERTVIEW_WIDTH  [UIScreen mainScreen].bounds.size.width-48*2
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define MARGIN  48

#import "SpecialAlertView.h"

@interface SpecialAlertView()<UITextFieldDelegate,ZXRegTextFieldDelegate>

@property(nonatomic,strong)UIView *alertView;
@property(nonatomic,strong)UITextField *fld;
@property(nonatomic,strong)NSString *pswStr;
@end

@implementation SpecialAlertView


-(instancetype) initWithLevelType:(BOOL )levelType{
    
    self = [super init];
    if (self) {
                
        if (levelType) {
            
            self.frame = [UIScreen mainScreen].bounds;
            self.alertView = [[UIView alloc]initWithFrame:CGRectMake(MARGIN, HEIGHT/2-ALERTVIEW_HEIGHT/2-50, WIDTH-MARGIN*2, ALERTVIEW_HEIGHT)];
            self.alertView.backgroundColor = [UIColor whiteColor];
            self.alertView.layer.cornerRadius = 8;
            self.alertView.layer.masksToBounds = YES;
            [self addSubview:self.alertView];
            
            UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 20)];
            lab.text = @"交易密码";
            lab.textColor = [UIColor colorForHex:@"#333333"];
            lab.textAlignment = NSTextAlignmentLeft;
            lab.font = [UIFont fontWithName:@"PingFang-SC-Bold" size:18];
            [self.alertView addSubview:lab];
            
            
            UIButton *canelBtn= [[UIButton alloc]initWithFrame:CGRectMake(WIDTH-MARGIN*2-60, 15, 40, 20)];
            canelBtn.tag = 100;
            [canelBtn setImage:[UIImage imageNamed:@"nav_cha"] forState:UIControlStateNormal];
            [canelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.alertView addSubview:canelBtn];
            
            ZXRegTextField *fld = [[ZXRegTextField alloc]initWithFrame:CGRectMake(15, 60, WIDTH-MARGIN*2-30, 44)];
            fld.textColor = [UIColor colorForHex:@"#333333"];
            fld.font = [UIFont fontWithName:@"PingFang-SC-Bold" size:18];
            fld.delegate = self;
            fld.zxdelegate = self;
            fld.zxType = ZXRegTextFieldTypeChars;
            fld.secureTextEntry = YES;
            fld.placeholder = @"请输入交易密码";
            fld.borderStyle = UITextBorderStyleNone;
            fld.textAlignment = NSTextAlignmentCenter;
            _fld = fld;
            [self.alertView addSubview:fld];

            UIView *vi = [[UIView alloc]initWithFrame:CGRectMake(15, 110, WIDTH-MARGIN*2-30, 1)];
            vi.backgroundColor = [UIColor colorForHex:@"#f0f0f0"];
            [self.alertView addSubview:vi];

            UIButton *forgetlBtn= [[UIButton alloc]initWithFrame:CGRectMake(15, 120, 100, 30)];
            forgetlBtn.tag = 110;
            [forgetlBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
            [forgetlBtn setTitleColor:[UIColor colorForHex:@"#999999"] forState:UIControlStateNormal];
            [forgetlBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            forgetlBtn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Bold" size:13];
            [self.alertView addSubview:forgetlBtn];
            UIImage * img= [UIImage imageNamed:@"11.png"];
            img = [img stretchableImageWithLeftCapWidth:1 topCapHeight:1];
            
         
            UIButton *sureBtn= [[UIButton alloc]initWithFrame:CGRectMake((WIDTH-MARGIN*2-164)/2, ALERTVIEW_HEIGHT-60, 164, 50)];
            sureBtn.tag = 120;
            [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
            [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [sureBtn setBackgroundImage:[UIImage  imageNamed:@"btn_jymmliang"] forState:UIControlStateNormal];
            [sureBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            sureBtn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Bold" size:14];
            [self.alertView addSubview:sureBtn];
        
        }else{
            

        }
       
        
        
    }
    [self showAnimation];
    return self;
}

-(void)showAnimation{
    
    self.backgroundColor = [UIColor clearColor];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
    
    self.alertView.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.2,0.2);
    self.alertView.alpha = 0;
    [UIView animateWithDuration:0.5 delay:0.1 usingSpringWithDamping:0.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.4f];
        self.alertView.transform = transform;
        self.alertView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

//-(void)textFieldDidBeginEditing:(UITextField *)textField{
//
//    textField.text = [Apputil replaceStringWithAsterisk:textField.text startLocation:0 lenght:textField.text.length];
//
//}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    _pswStr = textField.text;
    
}

-(void)btnClick:(UIButton *)sender{
    
    [_fld resignFirstResponder];
    
  if (sender.tag != 100) {
        
        if (self.sureClick) {
            self.sureClick(sender.tag,_pswStr,self);
        }
      
    }else{
        
        [UIView animateWithDuration:0.3 animations:^{
            [self removeFromSuperview];
        }];
    }
    
   
}

-(void)withSureClick:(sureBlock)block{
    
    _sureClick = block;
}



@end
